//商家
(function(){
	var config = require("../_include/config");
	var db = require("../db/index");
	var md5 = require("../_include/md5");
	//构造函数
	var _con = function (info){
		var self = this;
		//身份编号
		self._id = (info&&info.name)||(info.name="B_"+Math.random().toString(16).substr(2)+(+new Date).toString(16));
		//操作记录
		self._his = [];
		//额外信息
		self.info = info;
		//密码非明文存储
		self.info.password = md5.md5_2(self.info.password);
		//仓库
		self.store = [];
		//订单历史
		self.ach = [];
		db.insert(config.dbname.bus,self);
		console.log("注册了一个商家：",info.name);
	};
	//技能
	_con.prototype = require("./skill");
	module.exports = _con;
	//静态方法
	_con.get = function(Id){
		var result = db.find_by_id(config.dbname.bus,Id);
		if(result){
			result.__proto__ = _con.prototype;
		}
		return result;
	};
	_con.login = function(bus_info){
		bus_info.name = bus_info.name.toLowerCase();
		var mybey_bus = db.find_one(config.dbname.bus,{
			info:{
				name:bus_info.name
			}
		});
		if (!mybey_bus) {
			console.log("找不到名字为：“",bus_info.name,"”的商家！");
			throw "找不到指定的商家";
		}else if(mybey_bus.info.password!==md5.md5_2(bus_info.password)){
			console.log("密码错误！");
			throw "密码错误";
		}else{
			console.log(bus_info.name,"登陆成功");
			return mybey_bus;
		}
	};
}());